package com.imchen.cms.cmsweb.controller;

import com.imchen.cms.exception.CmsException;
import com.imchen.cms.model.WebFile;
import com.imchen.cms.service.WebFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.util.HashMap;

/**
 * @author imchen
 * @create: 2019-01-16 17:09
 **/
@RestController
@RequestMapping("/file")
public class WebFileController implements BaseController<WebFile> {

    @Autowired
    private WebFileService webFileService;

    @Override
    public HashMap add(@RequestBody WebFile webFile) throws CmsException {
        return webFileService.add(webFile);
    }

    @PostMapping("/upload")
    public WebFile upload(@RequestParam("file") MultipartFile file) throws CmsException {
        return webFileService.upload(file);
    }

    @GetMapping("/download/{fileName}")
    public int download(@PathVariable String fileName, HttpServletResponse response) throws CmsException {
        return webFileService.download(response,fileName);
    }

    @GetMapping(value = "/{fileName}",produces = MediaType.IMAGE_PNG_VALUE)
    public ResponseEntity<Resource>  view(@PathVariable String fileName, HttpServletResponse response) throws CmsException, FileNotFoundException {
        return webFileService.view(fileName);
    }

    @Override
    public int remove(int id) {
        return 0;
    }

    @Override
    public int delete(int id) {
        return 0;
    }

    @Override
    public int update(WebFile webFile) {
        return 0;
    }

    @Override
    public HashMap<String,Object> find(int id) {
        return null;
    }
}